package cn.linstudy.ware.service.impl;

import cn.linstudy.common.constant.enums.ware.PurchaseOrderDetailEnum;
import cn.linstudy.common.constant.enums.ware.PurchaseOrderEnum;
import cn.linstudy.ware.entity.PurchaseDetailEntity;
import cn.linstudy.ware.service.PurchaseDetailService;
import cn.linstudy.ware.vo.MergePurchaseOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.linstudy.common.utils.PageUtils;
import cn.linstudy.common.utils.Query;

import cn.linstudy.ware.dao.PurchaseDao;
import cn.linstudy.ware.entity.PurchaseEntity;
import cn.linstudy.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    PurchaseDetailService purchaseDetailService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnclaimed(Map<String, Object> params) {
        QueryWrapper<PurchaseEntity> purchaseEntityQueryWrapper = new QueryWrapper<>();
        purchaseEntityQueryWrapper.eq("status", PurchaseOrderEnum.NEW_CREATE.getCode())
                .or().eq("status", PurchaseOrderEnum.ASSIGNED.getCode());
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                purchaseEntityQueryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void mergePurchaseOrder(MergePurchaseOrderVo mergePurchaseOrderVo) {
        Long purchaseId = mergePurchaseOrderVo.getPurchaseId();
        // 看看是否携带采购单id，如果不携带自动创建一个新单进行合并
        if (purchaseId == null){
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(PurchaseOrderEnum.NEW_CREATE.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId(); // 如果purchaseId没有携带，即为空，那么purchaseId就是后面增加的自增id
        }
        List<Long> items = mergePurchaseOrderVo.getItems();
        final long finalPurchaseId  = purchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map(item -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(item);
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            purchaseDetailEntity.setStatus(PurchaseOrderDetailEnum.ASSIGNED.getCode());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);
        // 再更新一下更新时间
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);
    }

}